/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {
  let dummy = new ListNode() //新建空节点
  dummy.next = head //指向头节点
  let n1 = dummy //指针1
  let n2 = dummy //指针2
  for (let i = 0; i < n; i++) n2 = n2.next //先让n2往前走n位，找n2位置
  //n2找好位置后，开始循环n1,n2往后走
  while (n2.next !== null) { //n2走到空的前一个节点
    n1 = n1.next
    n2 = n2.next
  }
  // 当n2走到结尾null，n1是目标节点的前一个节点
  n1.next = n1.next.next //绕过目标节点
  return dummy.next
};